﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Einstein.Data;
using Einstein.Utility;
using DevComponents.DotNetBar;
using DevComponents.Editors;
using Einstein.Group.DAL;
using Einstein.Group.BLL;

namespace Einstein.Platform.Window.Basic
{
    public partial class Basic_File_New : Office2007Form
    {
        static UserInfoBLL bll = new UserInfoBLL();
        public Basic_File_New()
        {
            InitializeComponent();
            txtBirthday.Value = DateTime.Now;
            txtRegDate.Value = DateTime.Now;
            txtExpireDate.Value = DateTime.Now.AddYears(1);

            BindDLL();
            BindOrg();
        }

        #region  方法

        public void BindModel()
        {
            if (ModelID != null)
            {
                UserInfo info = bll.GetModelByID(new UserInfo { UserID = ModelID.ToString() });
                if (info != null)
                {
                    txtUserID.Text = info.UserID;
                    txtUserID.Enabled = false;
                    txtUserName.Text = info.UserName;
                    ddlDept.SelectedByValue(info.DeptID);
                    txtCardID.Text = info.CardID;
                    txtPhysicalID.Text = info.PhysicalID;
                    ddlSex.SelectedByValue(info.Sex);
                    ddlUserType.SelectedByValue(info.UserTypeID);
                    txtPhone.Text = info.PhoneNumber;
                    txtIdNO.Text = info.IdNo;
                    if (info.Birthday.HasValue)
                    {
                        txtBirthday.Value = info.Birthday.Value;
                    }
                    txtAddress.Text = info.Address;
                    txtPostCode.Text = info.PostCode;
                    txtOpenCode.Text = info.OpenCode;
                    if (info.RegDate.HasValue)
                    {
                        txtRegDate.Value = info.RegDate.Value;
                    }
                    if (info.ExpireDate.HasValue)
                    {
                        txtExpireDate.Value = info.ExpireDate.Value;
                    }
                }
            }
        }

        void BindDLL()
        {
            ddlSex.Items.Add(new ComboItem { Text = "男", Value = 1 });
            ddlSex.Items.Add(new ComboItem { Text = "女", Value = 0 });

            IList<UserTypeInfo> list = new UserTypeInfoDAL().SelectAll();
            if (list != null)
            {
                foreach (UserTypeInfo item in list)
                {
                    ddlUserType.Items.Add(new ComboItem { Text = item.UserTypeName, Value = item.UserTypeID });
                }
            }
        }

        void BindOrg()
        {
            IList<DeptInfo> list = new DeptInfoDAL().SelectAll();
            if (list != null)
            {
                foreach (DeptInfo item in list.Where(a => a.ParentID == 0))
                {
                    ddlDept.Items.Add(new ComboItem { Value = item.DeptID, Text = item.DeptName });
                    IList<DeptInfo> child = list.Where(a => a.ParentID == item.DeptID).ToList();
                    if (child != null)
                    {
                        AddTree(child, list, 1);
                    }
                }
            }
        }

        void AddTree(IList<DeptInfo> childList, IList<DeptInfo> mainList, int level)
        {
            foreach (DeptInfo item in childList)
            {
                ddlDept.Items.Add(new ComboItem { Value = item.DeptID, Text = " ".PadLeft(level * 5) + item.DeptName });
                IList<DeptInfo> child = mainList.Where(a => a.ParentID == item.DeptID).ToList();
                if (child != null)
                {
                    AddTree(child, mainList, level + 1);
                }
            }
        }

        void Add_Edit()
        {
            if (superValidator1.Validate())
            {
                UserInfoDAL dal = new UserInfoDAL();
                UserInfo info = new UserInfo();
                info.UserName = txtUserName.Text;
                info.DeptID = int.Parse(((ComboItem)ddlDept.SelectedItem).Value.ToString());
                info.CardID = txtCardID.Text;
                info.PhysicalID = txtPhysicalID.Text;
                info.UserTypeID = Convert.ToInt32(((ComboItem)ddlUserType.SelectedItem).Value);
                info.Sex = Convert.ToInt32(((ComboItem)ddlSex.SelectedItem).Value.ToString());
                info.PhoneNumber = txtPhone.Text;
                info.IdNo = txtIdNO.Text;
                info.Birthday = txtBirthday.Value;
                info.Address = txtAddress.Text;
                info.PostCode = txtPostCode.Text;
                info.OpenCode = txtOpenCode.Text;
                info.RegDate = txtRegDate.Value;
                info.ExpireDate = txtExpireDate.Value;
                info.UserID = txtUserID.Text.Trim(); 
                if(!IsEdit)
                {
                    info.CreateBy = GlobalContents.CurrentLoginName;
                }
                if (bll.ExistCardID(info,IsEdit))
                {
                    MyMessageBox.ShowMessage(this, "已经存在相同的卡号，请重试。");
                    txtCardID.Focus();
                    return;
                }
                if (bll.ExistPhysicalID(info, IsEdit))
                {
                    MyMessageBox.ShowMessage(this, "已经存在相同的原始卡号，请重试。");
                    txtPhysicalID.Focus();
                    return;
                }
                if (bll.ExistUserId(info, IsEdit))
                {
                    MyMessageBox.ShowMessage(this, "已经存在相同的用户编号，请重试。");
                    txtUserID.Focus();
                    return;
                }
                if (bll.ExistUserName(info, IsEdit))
                {
                    MyMessageBox.ShowMessage(this, "已经存在相同的用户名，请重试。");
                    txtUserName.Focus();
                    return;
                } 

                bool result = false;
                if (IsEdit)
                {
                   result = bll.Update(info);
                }
                else
                {
                    result = bll.Add(info);
                }
                if (result)
                {
                    this.DialogResult = DialogResult.OK;
                    this.Close();
                }
                else
                {
                    MyMessageBox.ShowMessage(this, "保存失败，请重试。");
                }
            }
        }

        /// <summary>
        /// 对象的编号
        /// </summary>
        public object ModelID { get; set; }

        bool IsEdit
        {
            get
            {
                return ModelID != null;
            }
        }
        #endregion

        private void btn_Click(object sender, EventArgs e)
        {
            ButtonX btn = sender as ButtonX;
            if (btn != null)
            {
                switch (btn.Name)
                {
                    case "btn_OK":
                        Add_Edit();
                        break;
                    case "btn_Cancel":
                        {
                            this.DialogResult = DialogResult.No;
                            this.Close();
                        }
                        break;
                }
            }
        }
    }
}
